/*
 * @作者: Fultom
 * @创建时间: 2021-10-27 17:41:09
 * @最近修改时间: 2025-07-14 14:17:53
 * @功能:页面导航 以及页面路径 公共类
 */

/** 内部方法 将键值对拼接成URL带参数 */
const _params2Url = (obj: any) => {
  const aUrl = []
  const fnAdd = function (key: string, value: string) {
    return key + '=' + value
  }
  for (const k in obj) {
    aUrl.push(fnAdd(k, obj[k]))
  }
  return aUrl.join('&')
}

/** 获取当前页面路由参数 */
export const getCurPageParams = () => {
  const routes: any = getCurrentPages() // 获取当前打开过的页面路由数组
  const curParam = routes[routes.length - 1].options //获取路由参数
  return curParam
}

export const AppNavigate = {
  /** 保留当前页跳转新页面 */
  push: (url: string, option?: any) => {
    let urlTemp = url
    if (option && Object.keys(option).length > 0) {
      urlTemp = `${urlTemp}?${_params2Url(option)}`
    }
    console.log('urlTemp:', urlTemp)
    uni.navigateTo({ url: urlTemp })
  },
  /** 返回上一个或 N 个页面 */
  back: (delta = 1) => {
    uni.navigateBack({ delta: delta })
  },
  /** 替换当前页跳转新页面 */
  replace: (url: string, option?: {}) => {
    let urlTemp = url
    if (option && Object.keys(option).length > 0) {
      urlTemp = `${urlTemp}?${_params2Url(option)}`
    }
    uni.redirectTo({ url: urlTemp })
  },
  /** 回到导航页 */
  switchTab: (url: string, option?: {}) => {
    let urlTemp = url
    if (option && Object.keys(option).length > 0) {
      urlTemp = `${urlTemp}?${_params2Url(option)}`
    }
    uni.switchTab({ url: urlTemp })
  },
  /** 关闭所有页面，打开到应用内的某个页面。 */
  reLaunch: (url: string, option?: {}) => {
    let urlTemp = url
    if (option && Object.keys(option).length > 0) {
      urlTemp = `${urlTemp}?${_params2Url(option)}`
    }
    uni.reLaunch({ url: urlTemp })
  },
  /**
   * 跳转到其他小程序
   * @param option
   */
  jumpMiniProgram: (option: { appId: string; path: string; success: (result: any) => void }) => {
    uni.navigateToMiniProgram({
      appId: option.appId,
      path: option.path,
      success: option.success,
    })
  },
  goWebView: (url: string, isRedirect = false) => {
    if (url != '') {
      if (isRedirect) {
        AppNavigate.replace(url)
      } else {
        AppNavigate.push(url)
      }
    }
  },
}

import loginPath from './paths/login'
import { AppToast } from '@/utils/publicMethod'

export const PATH_LOGIN = loginPath
